/**
 * Created by wenchuan.li on 2018/7/16 10:56.
 */
define(function () {

    var self,
        app = win.app,
        pageParameters,
        addressInfo = {
            GWAK001: '',// 客户编号
            GWAKL004: '',//客户名称
            GWAK025: '',// 区域县
            GWAK030: '',//电话
        },
        distributor = {
            GWAK001: '',// 客户编号
            GWAKL004: '',//客户名称
            GWAK028: '',// 地址
            GWAK030: '',//电话
        },
        sjztInfo = {
            GWBG001: '',// 商机主题编号
            GWBG002: '',// 客户编号
            GWBGL003: '',// 商机主题描述
            GWAK028: '',// 客户详细地址
        },
        subData = {
            GWBHID: '',
            GWBHNAME: '',//商机主题
            GWBH001: '1',//商机来源
            GWBH003: '',//拜访日期
            GWBH004: '',//起始时间
            GWBH005: '',//结束时间
            GWBH006: '',//拜访人员姓名
            GWBH007: '',//拜访人员职位
            GWBH008: '',//拜访人员角色
            GWBH009: '',//拜访地址
            GWBH010: '',//拜访目的
            GWBH011: '',//拜访内容
            GWBH012: '',//客户预算
            GWBH013: '',//预计购买时间
            GWBH014: '',//购买关键点
            GWBH015: '',//关键评估项
            GWBH016: '',//关联经销商编号
            GWBH017: '',//计划跟踪日期
            GWBH018: '',//下次拜访日期
            GWBH019: '',//下次拜访预计开始时间
            GWBH020: '',//下次拜访预计结束时间
            GWBH021: '',//下次拜访人员姓名
            GWBH022: '',//下次拜访人员职位
            GWBH023: '',//下次拜访人员角色
            GWBH024: '',//下次拜访地址
            GWBH025: '',//下次拜访目的
            GWBH026: '',//下次拜访内容
            GWBH027: '',//商机分类
            GWBH028: '',//商机阶段
            GWBH029: '',//商机评定原因说明
            GWBH030: '',//进入本阶段日期
            GWBH031: '',//成交几率
            GWBH032: '',//商机评定审核说明（主管回复）
            GWBH033: '',//备注
            GWBH034: '1',//维系方式
            GWBH035: 'N',//备案
            GWBHACTI: 'N',
            GWBHSJJE: 0,//商机金额
            GWAK001: '',//客户
        },
        sub_orders = [],
        sub_orders_old = [],
        sub_pgx = [],//评估项
        g_bffl = [],//拜访内容分类
        g_sjfl = [],
        g_jxs = [],
        g_sjjd = [],
        g_sjzt = [], //商机主题
        g_gwbh040 = []//领导批示
    ;

    function setCountAmount(c) {
        subData.GWBHSJJE = c;
    }

    var Bfjltb = app.Module.extend({

        init: function () {
            self = this;

            self._super({
                _readyBefore: function () {
                    pageParameters = self._pageParameters;
                    app.extend(subData, pageParameters.item);
                    self.getJxs();
                }
            }, win);
        },
        getSjfl: function () {//商机分类
            app.ajax({
                xurl: "api/wx/gwa/getSjfl",
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        g_sjfl.pushArray(data.data, true);
                        if (g_sjfl.length > 0) {
                            subData.GWBH027 = g_sjfl[0].GWAU001;
                        }
                    }
                }
            });
        },
        getSjjd: function () {//商机阶段
            app.ajax({
                xurl: "api/wx/gwa/getSjjd",
                xdata: {
                    GWAK025: ''
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        g_sjjd.pushArray(data.data, true);
                        if (g_sjjd.length > 0) {
                            subData.GWBH028 = g_sjjd[0].GWAV001;
                        }
                    }

                }
            });
        },
        getJxs: function () {//获取经销商信息
            app.ajax({
                xurl: "api/wx/gwa/getGwakBygwak001",
                xdata: {
                    GWAK001: subData.GWBH016
                },
                cb: function (result) {
                    if (result.ok) {
                        app.extend(distributor, result.data.data)
                    }
                }
            });
        },
        getGjpgx: function () {//关键评估项
            app.ajax({
                xurl: "api/wx/gwat161/getPgx",
                xdata: {
                    GWBLNAME: pageParameters.item.GWBHNAME,
                    //gwblid:pageParameters.item.gwbhid
                },
                cb: function (result) {
                    if (result.ok) {
                        var v = result.data.data;
                        sub_pgx.pushArray(v, true).addMap({old: 1});
                        subData.GWBH015 = '';
                        for (var i = 0; i < v.length; i++) {
                            subData.GWBH015 += v[i].GWBCL003 + "  "
                        }
                    }
                }
            });
        },
        getSjje: function () {//商品明细
            app.ajax({
                xurl: "api/wx/gwat161/gwat161Spmx",
                xdata: {
                    GWBKNAME: subData.GWBHNAME,
                    GWBHSEQ: subData.GWBHSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        var c = 0;
                        result.data.data.forEach(function (v) {
                            c += (Number(v.GWBK004) * Number(v.GWBK003));
                        });
                        subData.GWBHSJJE = c;
                        sub_orders_old.pushArray(result.data.data, true);
                    }
                }
            });
        },
        getBffl: function () {//获取拜访记录内容分类
            app.ajax({
                xurl: "api/wx/gwat161/getGwbfListByzz",
                cb: function (result) {
                    if (result.ok) {
                        g_bffl.pushArray(result.data.data, true);
                        if (subData.GWBH011.indexOf('{') != -1) {
                            var _gwbh011 = JSON.parse(subData.GWBH011);
                            g_bffl.forEach(function (v) {
                                v.msg = _gwbh011[v.GWBFL002];
                            });
                        }
                    }
                }
            });
        },
        _ready: function () {
            self.getBffl();
            new Vue({
                el: app.getByid(doc, 'gwat161Info'),
                data: {
                    sub: subData,
                    address: addressInfo,
                    distributor: distributor,
                    sjfl: g_sjfl,
                    jxs: g_jxs,
                    sjjd: g_sjjd,
                    g_sjzt: g_sjzt,
                    sjzt: sjztInfo,
                    bffls: g_bffl,
                    GWBH040S: g_gwbh040
                },
                methods: {
                    showDistributors: function () {
                        app.openPage({
                            src: 'hn-bfjltb/distributor.html',
                            argsKey: 'distributor_page',
                            args: {
                                GWBHNAME: subData.GWBHNAME,
                                cb: function (info) {
                                    app.extend(distributor, info);
                                    subData.GWBH016 = distributor.GWAK001;
                                    subData.GWBH035 = distributor.GWBE003;
                                }
                            }
                        });
                    },
                    showSjzt: function () {
                        app.openPage({
                            src: 'hn-bfjltb/sjzt.html',
                            args: {
                                GWBG002: subData.GWBHID,
                                cb: function (info) {
                                    app.extend(sjztInfo, info);
                                    subData.GWBHNAME = sjztInfo.GWBG001;
                                }
                            }
                        });
                    },
                    showBfrs: function () { // 拜访人
                        app.openPage({
                            src: 'hn-bfjltb/bfr.html',
                            args: {
                                GWBIID: subData.GWBHID,
                                GWBINAME: subData.GWBHNAME,
                                cb: function (bfr) {
                                    subData.GWBH006 = bfr.GWBI002;//拜访人员姓名
                                    subData.GWBH007 = bfr.GWBI005;//拜访人员职位
                                    subData.GWBH008 = bfr.GWBI001;//拜访人员角色
                                }
                            }
                        });
                    },
                    showGjpgx: function () { // 关键评估项
                        app.openPage({
                            src: 'hn-bfjltb/gjpgx.html',
                            args: {
                                //gwblid: subData.gwbhid,
                                GWBLNAME: subData.GWBHNAME,
                                pgx: sub_pgx,
                                cb: function (v) {
                                    subData.GWBH015 = '';
                                    for (var i = 0; i < v.length; i++) {
                                        if (v[i].ischecked) {
                                            subData.GWBH015 += v[i].GWBCL003 + "  "
                                        }
                                    }
                                }
                            }
                        });
                    },
                    showSpmx: function () { // 商品明细
                        app.openPage({
                            src: 'hn-bfjltb/spmx.html',
                            args: {
                                _from: 'edit',
                                orders: sub_orders,
                                ordersOld: sub_orders_old,
                                //bfjltb: subData,
                                GWAK001: subData.GWBHID,
                                GWBHNAME: subData.GWBHNAME,
                                GWBHACTI: subData.GWBHACTI,
                                GWBHSEQ: subData.GWBHSEQ,
                                updateAmount: function (c) {
                                    setCountAmount(c);
                                }
                            }
                        });
                    },
                    showJzxx: function () { // 竞争信息
                        app.openPage({
                            src: 'hn-bfjltb/jzxx.html',
                            args: {
                                GWAK001: subData.GWBHID,
                                GWBHNAME: subData.GWBHNAME,
                                GWBHACTI: subData.GWBHACTI
                            }
                        });
                    },
                    showHxbf: function () { // 后续拜访
                        if (subData.GWAK001 == '') {
                            app.toast('请选择客户');
                            return;
                        }
                        if (subData.GWBHNAME.trim() == '') {
                            app.toast('请选择商机主题');
                            return;
                        }
                        app.openPage({
                            src: 'hn-bfjltb/hxbf.html',
                            args: {
                                GWBIID: addressInfo.GWAK001,
                                hxbfData: subData,
                                GWBHACTI: subData.GWBHACTI
                            }
                        });
                    },
                    showmsg: function () {
                        app.toast('该拜访记录已经审核，无法修改');
                        return;
                    },
                    submitData: function () {
                        subData.GWAK001 = subData.GWBHID;
                        var _subData = app.cloneJSON(subData);
                        _subData.GWBH019 = _subData.GWBH019.replace('T', ' ');
                        _subData.GWBH020 = _subData.GWBH020.replace('T', ' ');
                        if (_subData.GWBH019.length == 16) {
                            _subData.GWBH019 += ':00';
                        }
                        if (_subData.GWBH020.length == 16) {
                            _subData.GWBH020 += ':00';
                        }

                        var _gwbh011 = {};
                        g_bffl.forEach(function (v) {
                            _gwbh011[v.GWBFL002] = v.msg;
                        });
                        _subData.GWBH011 = app.jsonString(_gwbh011);

                        app.ajax({
                            xurl: "api/wx/gwat161/gwat161Edit",
                            xdata: {
                                data: _subData,
                                pgx: sub_pgx,
                                orders: sub_orders,
                                oldorders: sub_orders_old
                            },
                            loading: true,
                            toJsonStr: true,
                            rules: {
                                'data.GWBH030': {
                                    nul: [false, '请填写进入阶段日期']
                                },
                                'data.GWBH031': {
                                    nul: [false, '请输入成交几率']
                                },
                                // pgx: {
                                //     type: 'array',
                                //     nul: [false, '请选择关键评估项']
                                // },
                                'data.GWBH010': {
                                    nul: [false, '请输入拜访目的']
                                },
                                'data.GWBH011': {
                                    nul: [false, '请输入沟通内容']
                                },
                                'data.GWBH006': {
                                    nul: [false, '请选择拜访人']
                                },
                                // orders: {
                                //     type: 'array',
                                //     nul: [false, '请添加商品明细']
                                // }
                            },
                            confirm: {
                                msg: '是否提交资料？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.alert({
                                        msg: '数据保存成功',
                                        ok: function () {
                                            pageParameters.cb();
                                            app.back(1);
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            });

        },
        _lazy: function () {
            this._super();
            self.getSjfl();
            self.getSjjd();
            self.getGjpgx();
            self.getSjje();

            if (subData.GWBH040 && subData.GWBH040.indexOf('[') != -1) {
                g_gwbh040.pushArray(JSON.parse(subData.GWBH040));
            }

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005
            });
        }
    });

    new Bfjltb();

});